<?php
import( 'de.ceus-media.framework.krypton.core.Logic' );
import( 'classes.model.Result' );
import( 'classes.model.Run' );
import( 'classes.model.ServerProject' );
import( 'classes.logic.Record' );
import( 'classes.logic.Attachment' );
import( 'classes.logic.Authentication' );
//import( 'de.ceus-media.framework.krypton.core.database.StatementBuilder' );
/**
 *	Logic of Test Case Results.
 *	@package		mv2.logic
 *	@extends		Framework_Krypton_Core_Logic
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@author			David Seebacher <dseebacher@gmail.com>
 *	@since			13.03.2007
 *	@version		0.1
 */
/**
 *	Logic of Test Case Results.
 *	@package		mv2.logic
 *	@extends		Framework_Krypton_Core_Logic
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@author			David Seebacher <dseebacher@gmail.com>
 *	@since			13.03.2007
 *	@version		0.1
 */
class Logic_Result extends Framework_Krypton_Core_Logic
{
	function addRecord( $caseId, $runId, $data, $prefix )
	{
		$config		= Framework_Krypton_Core_Registry::getStatic( 'config' );
		$session	= Framework_Krypton_Core_Registry::getStatic( 'session' );
		$result	= 0;
		$data	= $this->removePrefixFromFields( $data, $prefix );
		$this->validateForm( 'testing/result', 'addRecord', $data, $prefix );

		$changed		= 0;
		$attachmentId	= 0;
		$upload			= $_FILES['add_record_attachment'];
		if( $upload['name'] )
		{
			$fileName	= $config['paths']['attachments'].$upload['name'];
			$moved		= move_uploaded_file( $upload['tmp_name'], $fileName );
			if( !$moved )
				throw new Framework_Krypton_Exception_IO( 'attachment_could_not_been_stored' );
//			$data['title']			= isset( $data['title'] ) ? $data['title'] : "";
//			$data['description']	= isset( $data['description'] ) ? $data['description'] : "";
			$logic	= new Logic_Attachment();
			$attachmentId	= $logic->addAttachment( $upload['name'], $data['title'], $data['description'] );
			$changed	+= 4;
		}
		if( $data['comment'] )
			$changed	+= 2;

		$resultData	= Logic_Result::findResultByIds( $caseId, $runId );
		if( $resultData )
		{
			$resultId	= $resultData['result_id'];
			$status	= $resultData['status'];
			if( $status != $data['status'] )
			{
				$changed	+= 1;
			}
		}
		else
		{
			$resultData	= array(
				'case_id'	=> $caseId,
				'run_id'	=> $runId,
				'tester_id'	=> Logic_Authentication::getCurrentUser(),
				'status'	=> $data['status'],
			);
			$model	= new Model_Result();
			$resultId = $model->insert( $resultData );
			if( $data['status'] )
				$changed	+= 1;
		}
		if( $changed )
		{
			$logic	= new Logic_Record();
			$result = $logic->addRecord( $resultId, $data['status'], $data['comment'], $attachmentId );
			if( $changed % 2 )
			{
				$model	= new Model_Result( $resultId );
				$model->update( array( 'status' => $data['status'] ) );
				$this->removeCachedTree( $session->get( 'projectId' ) );
			}
		}
		return $result;
	}

	private function removeCachedTree( $projectId )
	{
		$config		= Framework_Krypton_Core_Registry::getStatic( 'config' );
		$fileName	= $config['paths']['cache']."testingTree_".$projectId.".cache";
		@unlink( $fileName );
	}

/*	function addResult( $data, $prefix = "" )
	{
		$data	= $this->removePrefixFromFields( $data, $prefix );
		$this->validateForm( 'management/result', 'addResult', $data, $prefix );
		$model	= new Model_Run( $data['run_id'] );
		if( !$model->exists() )
			throw new Framework_Krypton_Exception_Logic( 'invalid_run_id' );
		$model	= new Model_ServerProject( $data['server_project_id'] );
		if( !$model->exists() )
			throw new Framework_Krypton_Exception_Logic( 'invalid_server_project_id' );
		$model	= new Model_Result();
		$data['tester_id']	= Logic_Authentication::getCurrentUser();
		$data['created']	= time();
		return $model->insert( $data );
	}*/
	
	function editResult( $resultId, $data, $prefix = "" )
	{
		if( !$resultId )
		 	throw new Framework_Krypton_Exception_Logic( 'result_id_missing' );
		$model	= new Model_Result( $resultId );
		if( !$model->exists() )
			throw new Framework_Krypton_Exception_Logic( 'invalid_result_id' );
		$data	= $this->removePrefixFromFields( $data, $prefix );
/*		if( $data['status'] == 2 )
			$data['started']	= time();			
		else if( $data['status'] == 5 )
			$data['finished']	= time();*/
		$this->validateForm( 'management/result', 'editResult', $data, $prefix );
		$result	= $model->update( $data );
		if( $result )
			$model->update( array( 'modified' => time() ) );
		return $result;
	}

	static function findResultByIds( $caseId = false, $runId = false, $testerId = false, $status = false, $orders = array() )
	{
		$model	= new Model_Result();
		$conditions	= array();
		if( $caseId )
			$conditions['case_id']	= $caseId;
		if( $runId )
			$conditions['run_id']	= $runId;
		if( $testerId )
			$conditions['tester_id']	= $testerId;
		if( $status )
			$conditions['status']	= $status;
		if( !count( $conditions ) )
			throw new Framework_Krypton_Exception_Logic( 'at_least_one_parameter_needed' );
		$result	= $model->find( array(), $conditions, $orders );
		if( $result )
			$result = array_shift( $result );
		return $result;
	}

	static function getResultData( $resultId )
	{
		$model	= new Model_Result( $resultId );
		$data	= $model->get();
		if( !$data )
			throw new Framework_Krypton_Exception_Logic( 'invalid_result_id' );
		return $data;
	}
	
	static function getResultList( $runId, $serverProjectId, $filters )
	{
		$model	= new Model_Result();
		$conditions	= array(
			'run_id'			=> $runId,
			'server_project_id' => $serverProjectId,
			);
		$conditions	= array_merge( $conditions, $filters );
		$order		= array( 'created' => "ASC" );
		return $model->find( array(), $conditions, $order );
	}
}
?>
